import axios from 'axios';
import router from '../router/index';
import store from '../store'
if (store.state.urls.indexOf("/admin") != -1) {
  var bool = true;
} else {
  var bool = false;
};

import {
  Loading
} from 'element-ui';

var loadings = {}
axios.defaults.timeout = 9000000000;
axios.defaults.baseURL = process.env.API_ROOT       // 不同环境, 不同地址

axios.interceptors.request.use(
  config => {
     
    if(config.url == '/static/area.json') {
      config.baseURL = './'
    }else{
      config.baseURL = process.env.API_ROOT
    }

    // console.log(config)
    loadings = Loading.service({
      lock: true,
      text: '正在加载中...',
      spinner: 'el-icon-loading',
      background: 'rgba(0, 0, 0, 0.9)'
    });
    let token = sessionStorage.getItem('author_token');
    if (token) {
      config.headers.author_token = token;
    }
    // config.url = config.url + "?times=" + new Date().getTime();
    // config.url =config.url.slice(config.url.slice(config.url.indexOf("/")+1).indexOf("/")+1);
    return config;
    
  },
  err => {
    return Promise.reject(err);
  });

axios.interceptors.response.use(
  response => {
    // if (response.url !== "/gov/gov/tmProject/selectGaoDePoiByKeywords") {
    //   loadings.close();
    // }
    // if(response.url == "/static/area.json") {
    //   response.config.baseURL = '/'
    // }
    // console.log(response.config.baseURL)
    if (response) {
      loadings.close();
      let name = ""
      switch (response.data.status) {
        case 400:
          sessionStorage.clear();
          if (bool) {
            name = "login";
          } else {
            name = "login";
          }
          router.replace({
            name: name
          })
        case 401:
          sessionStorage.clear();
          if (bool) {
            name = "login";
          } else {
            name = "login";
          }
          router.replace({
            name: name
          })
      }
    }
    return response;
  },
  error => {
    // if (error.url !== "/gov/gov/tmProject/selectGaoDePoiByKeywords") {
    //   loadings.close();
    // }
    if(error) {
      loadings.close();
      return Promise.reject(error)
    }
  });
export default axios;
